package day9_26;

import java.util.Arrays;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/22 18:26
 */
public class TimeComplexity {
    public static void main(String[] args) {
        long[] arr = {10,9,8,76,5,4,2,0,-1,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
        for (int i = 0; i < arr.length; i++) {
            System.out.println(i+":"+binarySearch(arr,i));
        }
    }
    public static void bubbleSort(long[] array) {
        //外层循环，次数
        for (int i = 0; i < array.length - 1; i++) {
            boolean sorted = true;
            //内层循环，比较
            for (int j = 1; j < array.length - i ; j++) {
                if (array[j - 1] > array[j]) {
                    long temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                    sorted = false;
                }
            }
            if (sorted){
                break;
            }
        }
    }
    public static int binarySearch(long[] array,long target){
        int left = 0;
        int right = array.length-1;
        while (left<=right){
            int mid = left + (right-left)/2;
            if (target == array[mid]){
                return mid;
            }else if(target>array[mid]){
                left = mid+1;
            }else {
                right = mid-1;
            }
        }
        return -1;
    }
}
